﻿using System;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.InterceptionExtension;
using VerifyArgs;

namespace EP.Caching.Attributes {
	[Serializable]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
	public class CacheCallHandlerAttribute : HandlerAttribute {
		#region Fields / Properties
		private readonly CacheScope _scope;
		public CacheScope Scope { get { return _scope; } }
		#endregion

		#region Constructor
		public CacheCallHandlerAttribute()
			: this(CacheScope.Default) {
		}
		public CacheCallHandlerAttribute(CacheScope scope) {
			_scope = scope;
		}
		#endregion

		public override ICallHandler CreateHandler(IUnityContainer container) {
			Verify.Args(new { container }).NotNull();

			var callHandler = container.Resolve<CacheCallHandlerInvoke>();
			Verify.NotNull(new { callHandler });

			callHandler.Scope = this.Scope;

			return callHandler;
		}
	}
}
